package com.flea.hclv.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.flea.hclv.model.dao.HCarCalendarMapper;
import com.flea.hclv.model.entity.HCarCalendar;
import com.flea.hclv.service.HCarCalendarService;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.base.util.StringUtil;
import com.flea.system.utils.FleaStringUtils;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: project
 * @description: 车队日程表
 * @author: shiliqiang
 * @create: 2018-11-02 22:13
 * @version: 1.0
 */
@Service
public class HCarCalendarServiceImpl implements HCarCalendarService {

    @Autowired
    private HCarCalendarMapper hCarCalendarMapper;

    @Override
    public ServerResponse<List<HCarCalendar>> selectByMonth(String carId, String time) {
        if (carId==null){
            return ServerResponse.createByErrorMessage("carId为空");
        }
        List<HCarCalendar> list = hCarCalendarMapper.selectMonthByCarId(carId, time);
        return ServerResponse.createBySuccess(list);
    }

    @Override
    public ServerResponse<List<HCarCalendar>> selectCurrentMonth(String carId) {
        //查询当前月的日程的数据
        List<HCarCalendar> hCarCalendars = hCarCalendarMapper.selectCurrentMonthByCarId(carId);
        return ServerResponse.createBySuccess(hCarCalendars);
    }

    @Override
    public ServerResponse<String> updateList(List<HCarCalendar> hCarCalendars, String userId) {
        if (hCarCalendars==null){
            return ServerResponse.createByErrorMessage("参数为空");
        }
        int row = hCarCalendarMapper.updateList(hCarCalendars);
        if (row>0){
            return ServerResponse.createBySuccessMessage("操作成功");
        }
        return ServerResponse.createByErrorMessage("操作失败");
    }

    @Override
    public ServerResponse<String> insertList(String model, String userId) {
        if (StringUtil.isEmpty(model)) {
            return ServerResponse.createByErrorMessage("传入的参数为null");
        }
        JSONObject jsb = JSONObject.parseObject(model);
        if (null == jsb) {
            return ServerResponse.createByErrorMessage("参数格式化转换异常");
        }
        String carId = String.valueOf(jsb.get("carId"));
        if (StringUtil.isEmpty(carId)) {
            return ServerResponse.createByErrorMessage("车id参数为空，异常");
        }
        String timeTables = String.valueOf(jsb.get("carTimeTables"));
        if (StringUtil.isEmpty(timeTables)) {
            return ServerResponse.createByErrorMessage("车班期参数为空，异常");
        }
        List<HCarCalendar> hCarCalendars = JSON.parseArray(model,HCarCalendar.class);
        if (null == hCarCalendars || hCarCalendars.size() == 0) {
            return ServerResponse.createByErrorMessage("班期转换结果为空，异常");
        }
        List<HCarCalendar> dbTimeTables = hCarCalendarMapper.findByCarId(carId);
        for (HCarCalendar tbs: dbTimeTables
                ) {
            for (HCarCalendar tb_tm: hCarCalendars ) {

                if(DateUtils.isSameDay(tbs.getTodayTime(),tb_tm.getTodayTime())){
                    return  ServerResponse.createByErrorMessage("存在同一天的日程，请检查数据，并重新尝试");
                }

            }
        }
        List<HCarCalendar> list = this.assembleCarCalendars(hCarCalendars,userId,carId);
        int row = hCarCalendarMapper.insertList(list);
        if (row>0){
            return ServerResponse.createBySuccessMessage("操作成功");
        }
        return ServerResponse.createByErrorMessage("操作失败");

    }

    private List<HCarCalendar> assembleCarCalendars(List<HCarCalendar> hCarCalendars,String userId,String carId) {
        List<HCarCalendar> list = Lists.newArrayList();
        for (HCarCalendar hCarCalendar: hCarCalendars) {
            HCarCalendar calendar = new HCarCalendar();
            calendar.setId(FleaStringUtils.getGenerateUUID());
            calendar.setCreateUser(userId);
            //todo zhuangtai
//            calendar.setStatus(hCarCalendar.getStatus());
            calendar.setTodayTime(hCarCalendar.getTodayTime());
            calendar.setCarId(carId);
            list.add(calendar);
        }
        return list;
    }
}
